package de.uni.bremen.estudent.lbs.models;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.location.Location;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

import de.uni.bremen.estudent.lbs.util.Constants;

/**
 * The Overlay for the own position.
 * 
 * @author bahlfeld
 */
public final class MyPosOverlay extends Overlay {

    /**
     * Tag to identify the class.
     */
    private static final String TAG = "eStundenLBS - MyPosOverlay";
    /**
     * The geo location of the user.
     */
    private Location location;
    private final int mOldZoomLevel = 0;

    /**
     * Radius of the point in the mapview.
     */
    private final int mRadius = 9;

    /**
     * Draws the marker on the mapview.
     * 
     * @param canvas
     * @param mapView
     * @param shadow
     */
    @Override
    public void draw(final Canvas canvas, final MapView mapView,
            final boolean shadow) {
        Projection projection = mapView.getProjection();
        if (shadow == false && this.location != null) {
            Double latitude = this.location.getLatitude()
                    * Constants.GEO_POINT_UNIT;
            Double longtitude = this.location.getLongitude()
                    * Constants.GEO_POINT_UNIT;
            GeoPoint pos = new GeoPoint(latitude.intValue(),
                    longtitude.intValue());

            Point point = new Point();

            projection.toPixels(pos, point);

            RectF oval = new RectF(point.x - this.mRadius, point.y
                    - this.mRadius, point.x + this.mRadius, point.y
                    + this.mRadius);

            // Setup the paint for the red dot.
            final Paint paint = new Paint();
            paint.setARGB(255, 255, 0, 0);
            paint.setAntiAlias(true);

            // Backpaint
            final Paint backPaint = new Paint();
            backPaint.setARGB(180, 50, 50, 50);
            backPaint.setAntiAlias(true);

            // Textpaint
            final Paint textPaint = new Paint();
            textPaint.setARGB(255, 255, 255, 255);
            textPaint.setFakeBoldText(true);

            // rectangle for the textbackground
            RectF backRect = new RectF(point.x + 2 + this.mRadius, point.y - 3
                    * this.mRadius, point.x + 110, point.y + this.mRadius);

            // Draw the marker
            canvas.drawOval(oval, paint);
            canvas.drawRoundRect(backRect, 5, 5, backPaint);
            canvas.drawText("Mein Standort", point.x + 2 * this.mRadius,
                    point.y, textPaint);
        }
        super.draw(canvas, mapView, shadow);
    }

    /**
     * Returns the location field.
     * 
     * @return location can be null.
     */
    public Location getLocation() {
        return this.location;
    }

    /**
     * Sets the location field.
     * 
     * @param location
     */
    public void setLocation(final Location location) {
        Log.i(TAG, "Set location to " + location.toString());
        this.location = location;
    }

}
